﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace QueryBuilder.Core.Visualization
{
    /// <summary>
    /// 
    /// </summary>
    public static class NotifyPropertyChangedExtensions
    {
        /// <summary>
        /// Raises the specified handler.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="handler">The handler.</param>
        /// <param name="propertySelector">The property selector.</param>
        public static void Raise<TValue>(this PropertyChangedEventHandler handler, Expression<Func<TValue>> propertySelector)
        {
            if (handler != null)
            {
                var memberExpression = propertySelector.Body as MemberExpression;
                if (memberExpression != null)
                {
                    object sender = ((ConstantExpression) memberExpression.Expression).Value;
                    handler(sender, new PropertyChangedEventArgs(memberExpression.Member.Name));
                }
            }
        }
    }
}